<?php
namespace Admin\Controller;
use Think\Controller;

class StoresController extends HomeController {
    
    public function _initialize(){
        $storeList = D('Stores')->getStoresAllList();
        $this->assign('storeList',$storeList);
    }

	/*
	* Function:		storesList
	* Description:	场馆列表
	*/
    public function storesList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 10;
		$filter = returnFilter($filter);
		$list = D('Stores')->getStoresList($page,$filter,$pageSize);
		$count =  D('Stores')->getStoresCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$this->display();
    }
	
	/*
	* Function:		addStores
	* Description:	添加场馆
	*/
	public function addStores(){
		$ret = array();
		if(IS_POST){
			$data = I('post.');
			if(!empty($data['address'])){
			    $ret = analysisTxAddress($data['address']);
                $ret = json_decode($ret,true);
                $data['longitude'] = $ret['result']['location']['lng'];
                $data['latitude'] = $ret['result']['location']['lat'];
			}
			$data['content'] = stripslashes(htmlspecialchars_decode($data['content']));
			$data['addtime'] = date('Y-m-d H:i:s',time());
			if(D('Stores')->addStores($data)){
				$ret = array('code'=>1,'message'=>'添加成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'添加失败!');
				$this->ajaxReturn($ret);
			}
		}else{
		    $provinceList = M('regions')->where(['region_grade'=>1,'status'=>1])->select();
		    $this->assign('provinceList',$provinceList);
			$this->display();
		}
	}
	
	/*
	* Function:		editStores
	* Description:	编辑场馆
	* @param:		$id int id
	*/
	public function editStores($id = ''){
		if(IS_POST){
			$data = I('post.');
			if(!$info = D('Stores')->getStoresInfo($data['id'])){
			    $this->ajaxReturn(['code'=>0,'message'=>'不存在此记录']);
			}
			if(!empty($data['address'])){
			    if($data['address']!=$info['address']){
			        $ret = analysisTxAddress($data['address']);
                    $ret = json_decode($ret,true);
                    $data['longitude'] = $ret['result']['location']['lng'];
                    $data['latitude'] = $ret['result']['location']['lat'];
			    }
			}
			$data['content'] = stripslashes(htmlspecialchars_decode($data['content']));
			$ret = D('Stores')->updateStores($data);
			if($ret!==false){
				$ret = array('code'=>1,'message'=>'更新成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'更新失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			if(empty($id)){
				$this->error('参数异常');
			}else{
				$info = D('Stores')->getStoresInfo($id);
				$this->assign('info',$info);
				$this->display();
			}
		}
	}
	
	/*
	* Function:		deleteStores
	* Description:	删除场馆
	* @param:		$id
	*/
	public function deleteStores($id = ''){
	    if(!$info = D('Stores')->getStoresInfo($id)){
		    $this->ajaxReturn(['code'=>0,'message'=>'不存在此记录']);
		}
		$ret = M('stores_list')->where(['id'=>$id])->save(['is_deleted'=>1]);
		if($ret!==false){
			$ret = array('code'=>1,'message'=>'操作成功');
			$this->ajaxReturn($ret);
		}else{
			$ret = array('code'=>0,'message'=>'操作失败!');
			$this->ajaxReturn($ret);
		}
	}
	
	/*
	* Function:		classRoomList
	* Description:	教室列表
	*/
    public function classRoomList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 10;
		$filter = returnFilter($filter);
		$list = D('ClassRoom')->getClassRoomList($page,$filter,$pageSize);
		if(is_array($list)&&count($list)>0){
		    foreach($list as $key=>&$val){
		        $val['store_name'] = M('stores_list')->where(['id'=>$val['store_id']])->getField('title');
		    }
		}
		$count =  D('ClassRoom')->getClassRoomCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$this->display();
    }
	
	/*
	* Function:		addClassRoom
	* Description:	添加教室
	*/
	public function addClassRoom(){
		$ret = array();
		if(IS_POST){
			$data = I('post.');
			if(D('ClassRoom')->addClassRoom($data)){
				$ret = array('code'=>1,'message'=>'添加成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'添加失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			$this->display();
		}
	}
	
	/*
	* Function:		editClassRoom
	* Description:	编辑教室
	* @param:		$id int id
	*/
	public function editClassRoom($id = ''){
		if(IS_POST){
			$data = I('post.');
			$ret = D('ClassRoom')->updateClassRoom($data);
			if($ret!==false){
				$ret = array('code'=>1,'message'=>'更新成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'更新失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			if(empty($id)){
				$this->error('参数异常');
			}else{
				$info = D('ClassRoom')->getClassRoomInfo($id);
				$this->assign('info',$info);
				$this->display();
			}
		}
	}
	
	/*
	* Function:		deleteClassRoom
	* Description:	删除教室
	* @param:		$id
	*/
	public function deleteClassRoom($id = ''){
	    if(!$info = D('ClassRoom')->getClassRoomInfo($id)){
		    $this->ajaxReturn(['code'=>0,'message'=>'不存在此记录']);
		}
		$ret = M('stores_classroom_list')->where(['id'=>$id])->save(['is_deleted'=>1]);
		if($ret!==false){
			$ret = array('code'=>1,'message'=>'操作成功');
			$this->ajaxReturn($ret);
		}else{
			$ret = array('code'=>0,'message'=>'操作失败!');
			$this->ajaxReturn($ret);
		}
	}
	
	/*
	* Function:		workerList
	* Description:	工作列表
	*/
    public function workerList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 10;
		$filter = returnFilter($filter);
		$list = D('Worker')->getWorkerList($page,$filter,$pageSize);
		if(is_array($list)&&count($list)>0){
		    foreach($list as $key=>&$val){
		        $val['store_name'] = M('stores_list')->where(['id'=>$val['store_id']])->getField('title');
		        if($val['role']==1){
		            $val['role_name'] = '教练';
		        }else if($val['role']==2){
		            $val['role_name'] = '员工';
		        }else if($val['role']==3){
		            $val['role_name'] = '店长';
		        }
		    }
		}
		$count =  D('Worker')->getWorkerCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$this->display();
    }
	
	/*
	* Function:		addWorker
	* Description:	添加教室
	*/
	public function addWorker(){
		$ret = array();
		if(IS_POST){
			$data = I('post.');
			$data['content'] = stripslashes(htmlspecialchars_decode($data['content']));
			if(D('Worker')->addWorker($data)){
				$ret = array('code'=>1,'message'=>'添加成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'添加失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			$this->display();
		}
	}
	
	/*
	* Function:		editWorker
	* Description:	编辑教室
	* @param:		$id int id
	*/
	public function editWorker($id = ''){
		if(IS_POST){
			$data = I('post.');
			$data['content'] = stripslashes(htmlspecialchars_decode($data['content']));
			$ret = D('Worker')->updateWorker($data);
			if($ret!==false){
				$ret = array('code'=>1,'message'=>'更新成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'更新失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			if(empty($id)){
				$this->error('参数异常');
			}else{
				$info = D('Worker')->getWorkerInfo($id);
				$this->assign('info',$info);
				$this->display();
			}
		}
	}
	
	/*
	* Function:		deleteWorker
	* Description:	删除教室
	* @param:		$id
	*/
	public function deleteWorker($id = ''){
	    if(!$info = D('Worker')->getWorkerInfo($id)){
		    $this->ajaxReturn(['code'=>0,'message'=>'不存在此记录']);
		}
		$ret = M('stores_worker_list')->where(['id'=>$id])->save(['is_deleted'=>1]);
		if($ret!==false){
			$ret = array('code'=>1,'message'=>'操作成功');
			$this->ajaxReturn($ret);
		}else{
			$ret = array('code'=>0,'message'=>'操作失败!');
			$this->ajaxReturn($ret);
		}
	}
}